package com.smfx.yxt.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.CommentType;
import com.smfx.yxt.common.enums.RelationType;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.common.enums.UserRole;
import com.smfx.yxt.model.AttachmentList;
import com.smfx.yxt.model.ClassAlbum;
import com.smfx.yxt.model.ClassAlbumComment;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.ClassAlbumReqVO;
import com.smfx.yxt.reqVo.StudentReqVO;
import com.smfx.yxt.service.IAttachmentListService;
import com.smfx.yxt.service.IClassAlbumCommentService;
import com.smfx.yxt.service.IClassAlbumService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.view.ClassAlbumView;
import com.smfx.yxt.view.TermView;
import com.smfx.yxt.wechat.utils.WechatGlobalMap;

/**
 * 班级相册
 * @author huqimeng
 * @date 2017年5月25日
 */
@Controller
public class ClassAlbumController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(ClassAlbumController.class);
	
	@Autowired
	private IAttachmentListService attachmentListService;
	@Autowired
	private IClassAlbumService classAlbumService;
	@Autowired
	private IClassAlbumCommentService classAlbumCommentService;

	@RequestMapping(Const.TEACHER_ROLE + MenuConst.ABLUM_CLASS_URL)
	public String classAlbumForTeacher(Model model){
		model.addAttribute("grade", request.getParameter("grade"));
		model.addAttribute("clazz", request.getParameter("clazz"));
		return "album/classAlbum";
	}
	
	@RequestMapping(Const.STUDENT_ROLE + MenuConst.ABLUM_CLASS_URL)
	public String classAlbumForStudent(){
		return "album/classAlbum";
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.ABLUM_CLASS_URL + "/publish")
	public String classAlbumPublish(Model model) {
		model.addAttribute("relationType", RelationType.CLASS_ALBUM.getCode());
		return "album/publish";
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.ABLUM_CLASS_URL + "/edit/{classAlbumId}")
	public String classAlbumEdit(Model model, @PathVariable Integer classAlbumId) {
		AppUser appUser = getAppUser();
		ClassAlbumView detail = classAlbumService.selectViewById(classAlbumId);
		if(detail.getTeacherId().intValue() != appUser.getTeacher().getId()) {
			return null;
		}
		model.addAttribute("detail", detail);
		return "album/edit";
	}
	
	@RequestMapping(Const.TEACHER_ROLE + "/album/attchment/handing")
	@ResponseBody Object albumAttchmentHanding() {
		try {
			AppUser appUser = getAppUser();
			AttachmentList record = new AttachmentList();
			record.setTeacherId(appUser.getTeacher().getId());
			record.setRelationType(RelationType.CLASS_ALBUM.getCode());
			return attachmentListService.selectHandingList(record);
		} catch (Exception e) {
			logger.error("----albumAttchmentHanding error={}", e);
		}
		return null;
	}
	
	@RequestMapping(Const.TEACHER_ROLE + "/album/create")
	@ResponseBody Object albumCreate(ClassAlbumReqVO reqVO){
		try {
			AppUser appUser = getAppUser();
			ClassAlbum record = BeanUtils.copyAs(reqVO, ClassAlbum.class);
			record.setSchoolId(appUser.getTeacher().getSchoolId());
			record.setSchoolName(appUser.getTeacher().getSchoolName());
			record.setTermId(((TermView)WechatGlobalMap.get(Const.CACHE_TERM).getValue()).getId());
			record.setTeacherId(appUser.getTeacher().getId());
			record.setTeacherName(appUser.getTeacher().getName());
			String[] array = reqVO.getGradeClass().split("_");
			record.setGrade(Integer.valueOf(array[0]));
			record.setClazz(Integer.valueOf(array[1]));
			classAlbumService.createClassAlbum(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---noticeCreate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	/**
	 * 分页查询相册
	 * @param vo
	 * @return
	 */
	@RequestMapping("album/page/query")
	@ResponseBody Object pageQuery(StudentReqVO vo) {
		try {
			AppUser appUser = getAppUser();
			if(appUser.getRoleType() == UserRole.PARENT.getCode()) {
				vo.setParentOpenid(appUser.getParent().getOpenid());
			}
			Page<ClassAlbumView> page = new Page<ClassAlbumView>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("CREATE_TIME", "desc"));
			page.setSorts(sorts);
			vo.setRelationType(RelationType.CLASS_ALBUM.getCode());
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<ClassAlbumView> list = classAlbumService.selectListByPage(page);
			grid.setRows(list);
			grid.setTotal(page.getTotalRecord());
			return grid;
		} catch (Exception e) {
			logger.error("--pageQuery error={}", e);
		}
		return null;
	}
	
	@RequestMapping("album/comment")
	@ResponseBody Object albumCommet(ClassAlbumComment comment){
		try {
			AppUser appUser = getAppUser();
			if(appUser.getRoleType() == UserRole.TEACHER.getCode()) {
				comment.setName(appUser.getTeacher().getName());
			}else {
				comment.setName(appUser.getParent().getName());
			}
			comment.setOpenid(appUser.getOpenid());
			classAlbumCommentService.insertSelective(comment);
			return new RspResult(Status.SUCCESS, comment);
		} catch (Exception e) {
			logger.error("--albumCommet error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("album/comment/cancel")
	@ResponseBody Object albumCommetCancel(Integer albumId){
		try {
			AppUser appUser = getAppUser();
			ClassAlbumComment comment = new ClassAlbumComment();
			comment.setAlbumId(albumId);
			comment.setOpenid(appUser.getOpenid());
			comment.setType(CommentType.ZAN.getCode());
			comment.setIsDelete("Y");
			comment.setUpdateTime(new Date());
			classAlbumCommentService.updateCancelZan(comment);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("--albumCommet error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping(Const.TEACHER_ROLE + MenuConst.ABLUM_CLASS_URL + "/update")
	@ResponseBody Object albumUpdate(ClassAlbum record) {
		try {
			AppUser appUser = getAppUser();
			ClassAlbum classAlbum = classAlbumService.selectByPrimaryKey(record.getId());
			if(classAlbum.getTeacherId().intValue() == appUser.getTeacher().getId()) {
				classAlbum.setContent(record.getContent());
				classAlbumService.updateByPrimaryKeySelective(classAlbum);
				return new RspResult(Status.SUCCESS);
			}
		} catch (Exception e) {
			logger.error("---albumUpdate error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
}
